package service

import (
	"context"
	"fmt"
	__ "user-srv/basic/proto"
	"user-srv/handler/dao"
)

// 会员添加
func (s *Server) MemberLevelAdd(_ context.Context, in *__.MemberLevelAddReq) (*__.MemberLevelAddResp, error) {

	if in.Level < 0 || in.Name == "" || in.Price < 0 || in.Desc == "" || in.BenefitId < 0 {
		return nil, fmt.Errorf("参数错误")
	}

	add, err := dao.MemberLevelAdd(in)
	if err != nil {
		return nil, fmt.Errorf(err.Error())
	}

	return &__.MemberLevelAddResp{
		Id: add.Id,
	}, nil
}

// 会员展示
func (s *Server) MemberLevelFind(_ context.Context, in *__.MemberLevelFindReq) (*__.MemberLevelFindResp, error) {
	if in.Page < 0 || in.PageSize < 0 {
		return nil, fmt.Errorf("参数错误")
	}
	level, err := dao.MemberLevelFind(in)
	if err != nil {
		return nil, fmt.Errorf(err.Error())
	}

	return &__.MemberLevelFindResp{
		Lists: level,
	}, nil
}

// 会员详情
func (s *Server) MemberLevelDetail(_ context.Context, in *__.MemberLevelDetailReq) (*__.MemberLevelDetailResp, error) {
	if in.Id < 0 {
		return nil, fmt.Errorf("参数错误")
	}
	detail, err := dao.MemberLevelDetail(in)
	if err != nil {
		return nil, fmt.Errorf(err.Error())
	}

	return &__.MemberLevelDetailResp{
		Id:        detail.Id,
		BenefitId: detail.BenefitId,
		Name:      detail.Name,
		Price:     float32(detail.Price),
		Level:     detail.Level,
		Desc:      detail.Desc,
	}, nil
}

// 会员删除
func (s *Server) MemberLevelDel(_ context.Context, in *__.MemberLevelDelReq) (*__.MemberLevelDelResp, error) {
	if in.Id < 0 {
		return nil, fmt.Errorf("参数错误")
	}
	_, err := dao.MemberLevelDel(in)
	if err != nil {
		return nil, fmt.Errorf(err.Error())
	}

	return &__.MemberLevelDelResp{}, nil
}
